<link rel="stylesheet" href="*ADMIN*/wangEditor/css/wangEditor.min.css">
<script type="text/javascript" src="*ADMIN*/wangEditor/js/wangEditor.min.js"></script>
<textarea id="textarea" name="content" placeholder="请输入内容" class="layui-textarea" style="height:400px;width: 100%;"></textarea>
<script type="text/javascript">
    var editor = new wangEditor('textarea');
	editor.config.uploadImgUrl = '{:url("admin/up/Pic")}';
	editor.config.uploadImgFileName = 'FileName';
	// 关闭菜单栏fixed
    editor.config.menuFixed = false;
    // 阻止输出log
	wangEditor.config.printLog = false;
	// 关闭js过滤
    editor.config.jsFilter = false;
    // 取消粘贴过滤
    editor.config.pasteFilter = false;
    editor.config.menus =     [
        'source',
        '|',
        'bold',
        'underline',
        // 'italic',
        'strikethrough',
        'eraser',
        'forecolor',
        // 'bgcolor',
        'quote',
        // 'fontfamily',
        'fontsize',
        // 'head',
        // 'unorderlist',
        // 'orderlist',
        'alignleft',
        'aligncenter',
        'alignright',
        'link',
        'unlink',
        'table',
        'emotion',
        'img',
        'video',
        'location',
        'insertcode',
        'undo',
        'redo',
        'fullscreen'
    ];
    editor.create();
    // 获取编辑器区域完整html代码
    // var html = editor.$txt.html();
    // 获取编辑器纯文本内容
    // var text = editor.$txt.text();
    // 获取格式化后的纯文本
    // var formatText = editor.$txt.formatText();
    // 防止内容html标签被直接解析
    setTimeout(function () {getIframeContent()}, 500);
    function getIframeContent(){ 
      var frameObj = document.getElementById('iframe'); 
      var frameContent = frameObj.contentWindow.document.body.innerHTML; 
      $('.wangEditor-txt').html(frameContent);
      $('#textarea').val(frameContent);
    }
    layui.use(['form', 'upload', 'jquery','layer']
    ,function(){
        var form = layui.form,
        upload = layui.upload,
        layer = layui.layer,
        jq = layui.jquery;
        //上传附件
    	upload.render({
    		elem: '#scfujian',
    		url: "{:url("admin/up/up")}",
    		accept: 'file',
    		done: function(res) {
    		    if (res.code == '1') {
    		        var html = editor.$txt.html();
		    	    // 添加到容器
                    var contents = '<a href="'+res.path+'" class="sbtn" style="margin-right: 10px;border: 1px solid #ffa400;color: #ffa400;height: 36px;line-height: 36px;font-size: 15px;float: left;padding: 0 20px 0 20px;border-radius: 5px;" target="_blank" rel="nofollow">'+res.filename+'</a>';
                    editor.$txt.append(contents);
                    // 更新到textarea
                    $("#textarea").val(html+contents);
    		    	return layer.msg(res.msg);
    	    	} else {
    		    	return layer.msg(res.msg);
    	    	}
    		}
    	});
    });
    //插入外链地址
    layui.use('layer', function(){
      var layer = layui.layer;
        // 重置内容
        $("#chongzhi").click(function(){
            var url=$('#chongzhi').attr("url");
            $.ajax({
              url: url,
              type: "POST",
              dataType: "json", 
                success: function(data) {
                  if(data.code==1){
                    layer.msg(data.msg, {icon: 1,anim: 2, time: 1000}, function(){
                      $('.wangEditor-txt').html(data.com);
                      $('#textarea').val(data.com);
                    });
                  } else {
                    layer.msg(data.msg, {icon: 2, anim: 2, time: 1000});
                  }
                },
                error : function() {
                    layer.msg('异常', {icon: 2, anim: 2, time: 1000});
                }
            }); 
        });
        // 附件分隔符
        $("#fujianfgf").click(function(){
            // 添加到容器
            var contents = '<div><br></div><style>.fujianxiazai{position:relative;height:45px;border-bottom:1px solid#f6f6f6;text-indent:15px;font-size:16px;line-height:35px}.fujianxiazai:after{position:absolute;top:10px;left:0;width:5px;height:16px;border-radius:5px;background:linear-gradient(180deg,#ffc55d 0,#ffa400 100%);content:" "}</style><div class="fujianxiazai" id="dwon">资源下载</div><p></p>';
            var html = editor.$txt.html();
            editor.$txt.append(contents);
            // 更新到textarea
            $("#textarea").val(html+contents);
            layer.msg('插入成功');
        });
        // 附件外链
        $("#tianjialianjie").click(function(){
        layer.open({
            type: 1,
            title: false,
            skin: 'layui-layer-rim', //加上边框
            area: ['250px', '220px'], //宽高
            content: '<div align="center" style="padding: 40px 10px 10px 10px;"><input type="text" id="titleY" value="网盘下载" lay-verify="required" class="layui-input"><input type="text" style="margin-top: 10px;" placeholder="地址" id="textY" value="" lay-verify="required" class="layui-input"></div>',
            scrollbar: false,
            btn: ['提交', '取消']
            ,yes: function(index){
              layer.close(index); 
              var val=$('#textY').val();
              var title=$('#titleY').val();
              var html = editor.$txt.html();
              // 添加到容器
              var contents = '<a href="'+val+'" class="sbtn" style="margin-right: 10px;border: 1px solid #ffa400;color: #ffa400;height: 36px;line-height: 36px;font-size: 15px;float: left;padding: 0 20px 0 20px;border-radius: 5px;" target="_blank" rel="nofollow">'+title+'</a>';
              editor.$txt.append(contents);
              // 更新到textarea
              $("#textarea").val(html+contents);
              layer.msg('插入成功');
            }
          });
      });
       
    }); 
</script>
{:hook('admineditor')}